library riki_router;

import 'package:flutter/material.dart';
import 'package:riki_router/event/riki_event.dart';
import 'package:riki_router/route/riki_route_index.dart';
import 'package:riki_router/utils/riki_event_log_util.dart';
import 'package:riki_router/utils/riki_router_log_util.dart';

export 'core/riki_route_core.dart';
export 'route/riki_route_index.dart';
export 'event/riki_event.dart';
export 'package/riki_base_package.dart';

class RikiRouter {
  // 工厂模式
  factory RikiRouter() => _getInstance();

  static RikiRouter get instance => _getInstance();
  static RikiRouter? _instance;

  static late BuildContext _context;

  set context(BuildContext context) => _context = context;

  BuildContext get context => _context;

  RikiRouter._internal();

  static RikiRouter _getInstance() {
    if (_instance == null) {
      _instance = RikiRouter._internal();
    }
    return _instance!;
  }

  bool _isInit = false;

  late RikiRouterDelegate _routerDelegate;

  RikiRouterDelegate get routerDelegate => _routerDelegate;

  late RikiRouteInformationParser _routeInformationParser;

  RikiRouteInformationParser get routeInformationParser => _routeInformationParser;

  /// 初始化riki_router
  /// [routerDelegate] 路由代理
  /// [routeInformationParser] route数据解析
  /// [debug] 调试模式
  void init(
    RikiRouterDelegate routerDelegate, {
    RikiRouteInformationParser? routeInformationParser,
    bool debug = false,
  }) {
    if (_isInit) return;
    _isInit = true;
    RikiRouteConvert.init();
    RikiRouterLogUtil.init(isDebug: debug);
    RikiEvent.instance.init(debug: debug);
    _routerDelegate = routerDelegate;
    _routeInformationParser = routeInformationParser ?? RikiRouteInformationParser();
  }

  /// 拓展转换
  /// [convert] 转换方法
  void expandConvert(OnRikiRouterConvert convert) {
    RikiRouteConvert.expand(convert);
  }
}
